// pages/userConsole/userConsole.js
import Notify from '@vant/weapp/notify/notify';
import * as echarts from '../../ec-canvas/echarts';
const db = wx.cloud.database()
const _ = db.command

let chart = null;

function initChart(canvas, width, height, dpr) {
  chart = echarts.init(canvas, null, {
    width: width,
    height: height,
    devicePixelRatio: dpr // new
  });
  canvas.setChart(chart);

  return chart;
}

Page({

  data: {
    shows: 0,
    show2: false,
    minDate: new Date().getTime() - 864000000,
    defaultdate: new Date().getTime(),
    maxDate: new Date().getTime(),
    loading: false,
    wuliao: "",
    didi: "",
    yongtu: "",
    xinghao: "",
    ec: {
      onInit: initChart
    },
    typedatas: [{
        name: '醴陵三三',
        icon: '../../icon/gongzi.png',
        active: false
      },
      {
        name: '无锡宝通',
        icon: '../../icon/jianzhi.png',
        active: false
      },
      {
        name: '河北一川',
        icon: '../../icon/lijin.png',
        active: false
      },
      {
        name: '福建明信',
        icon: '../../icon/jiangjin.png',
        active: false
      },

    ],
    show: false,
    date: '',
    message: '',
    money: '1',
    expenditure: true,
    _openid: "",
    type: 1,
    recordtype: '醴陵三三',
    icondata: '',
    steppervalue: 1
  },
  onLoad: function () {
    let that = this
    wx.cloud.callFunction({
      name: "login",
      complete: res => {
        this.setData({
          _openid: res.result.openid
        });
      }
    })
  },
  getLastDay(data) {
    console.log(this.data.ck_name)
    var option = {
      tooltip: {
        trigger: 'axis',
        axisPointer: {
          type: 'shadow'
        }
      },
      legend: {
        data: ['进仓', '出库'],
        left:"left"
      },
      dataZoom: [//滑动条
        {
            yAxisIndex: 0,//这里是从X轴的0刻度开始
            show: false,//是否显示滑动条，不影响使用
            type: 'slider', // 这个 dataZoom 组件是 slider 型 dataZoom 组件
            startValue: 0, // 从头开始。
            endValue: 6  // 一次性展示6个。
        }
    ],
      color: ['#37a2da', '#32c5e9', '#67e0e3'],
      grid: {
        left: '3%',
        right: '4%',
        bottom: '3%',
        containLabel: true
      },
      textStyle: {

        color: '#909399'
      },
      xAxis: {
        type: 'value',
        boundaryGap: [0, 0.01],
    
      },
      yAxis: {
        type: 'category',
        data: data.name,
     
      },
      series: [{
          name: '进仓',
          type: 'bar',
          data: data.jc_shul
        },
        {
          name: '出库',
          type: 'bar',
          data: data.ck_shul,
        }
      ]
    };
    setTimeout(() => {
      chart.clear()
      chart.setOption(option);
    }, 1500)
  },
  onShow: function (options) {


    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({
        id: 3
      })

    }
    this.setData({
      shows: true,
      date: '',
      message: '',
      steppervalue: 1
    });
    this.shuju()


  },
  show3cl() {
    console.log(1)
    this.setData({
      show3: true
    })
  },
  shuju() {
    db.collection('ck').where({
        userid: wx.getStorageSync('id')
      }).get().then(res => {
        let data = new Array
        data.name = []
        data.jc_shul = []
        data.ck_shul = []
        res.data.forEach(e => {
          data.ck_shul.push(e.ck_shuliang)
          data.jc_shul.push(e.jc_shuliang)
          data.name.push(e.wuliao)
        })
        this.setData({
          ck_list: res.data,

        })
        console.log(data.ck_shul)
        this.getLastDay(data)

      }),
      db.collection('record').where({
        money: _.gt(0),
        userid: wx.getStorageSync('id')
      }).get().then(res => {
        let money = 0
        for (let a of res.data) {
          money += Number(a.money)
        }
        this.setData({
          jcdata: money,
          ckdata: res.data
        })

      })

  },

  shuju2() {


  },
  showPopup() {
    this.setData({
      show2: true
    });
  },
  onClose2() {
    this.setData({
      show2: false
    });
  },
  onClose3() {
    this.setData({
      show3: false
    });
  },
  onClick2(event) {
    const {
      name
    } = event.currentTarget.dataset;
    console.log(name)
    this.setData({
      recordtype: name,
    });
  },
  onClick3(event) {
    const {
      name,
      item
    } = event.currentTarget.dataset;
    this.setData({
      wuliao: name,
      jc_wl: item.userid,
      date: item.date,
      didi: item.didi,
      money: item.money,
      recordtype: item.recordtype
    });
    console.log(item, name, )
  },
  onChange2(event) {
    console.log(event.detail)
    this.setData({
      recordtype: event.detail,
    });
  },

  ceshi(event) {
    let that = this
    console.log(event.currentTarget.dataset.message)
    that.setData({
      recordtype: event.currentTarget.dataset.message,
      icondata: event.currentTarget.dataset.icon
    });
    let typelist = that.data.type == '0' ? that.data.typedata : that.data.typedatas
    for (var i = 0; i < typelist.length; i++) {
      typelist[i].active = false;
      that.data.type == '0' ? that.setData({
        typedata: typelist
      }) : that.setData({
        typedatas: typelist
      })
      if (typelist[i].name == event.currentTarget.dataset.message) {
        typelist[i].active = true;
        that.data.type == '0' ? that.setData({
          typedata: typelist
        }) : that.setData({
          typedatas: typelist
        })
      }
    }
  },
  calendar() {
    this.setData({
      show: true
    });
  },
  onDisplay() {
    this.setData({
      show: true
    });
  },
  onClose() {
    this.setData({
      show: false
    });
  },
  formatDate(date) {
    const dt = new Date(date);
    const y = dt.getFullYear();
    const m = (dt.getMonth() + 1 + "").padStart(2, "0");
    const d = (dt.getDate() + "").padStart(2, "0");
    return `${y}-${m}-${d}`;
  },
  onConfirm(event) {
    this.setData({
      show: false,
      date: this.formatDate(event.detail),
    });
  },
  onInput(event) {
    this.setData({
      currentDate: event.detail,
    });
  },
  onChange(event) {
    this.setData({
      money: event.detail
    });
  },
  add_pf() {
    for (let a of this.data.ckdata) {
      if (this.data.wuliao == a.wuliao) {
        return false
      }
    }
  },
  magdata(e) {
    this.setData({
      message: e.detail
    });
  },
  preservation2() {
    let postdata = {
      wuliao: this.data.wuliao,
      xinghao: this.data.xinghao,
      userid: wx.getStorageSync('id')
    }
    db.collection('ck').add({
      data: postdata
    }).then(res => {
      Notify({
        type: 'success',
        message: '新增物料仓成功'
      });
      this.shuju()
    })
  },
  preservation() {
    if (!this.data.recordtype) {
      Notify({
        type: 'warning',
        message: '请选择类型哦！'
      });
    } else if (!this.data.date) {
      Notify({
        type: 'warning',
        message: '请选择日期哦！'
      });
    } else if (!this.data.money) {
      Notify({
        type: 'warning',
        message: '请输入数量哦！'
      });
    } else {
      if (this.data.jc_wl) {
        let postdata = {
          type: this.data.type,
          recordtype: this.data.recordtype,
          didi: this.data.didi,
          wuliao: this.data.wuliao,
          yongtu: this.data.yongtu,
          date: this.data.date,
          remarks: this.data.message,
          money: this.data.type == '0' ? this.data.money * -1 : Number(this.data.money),
          userid: wx.getStorageSync('id'),
          xg: true
        }
        db.collection('record2').where({
          _id: this.data.jc_wl
        }).update({
          data: {
            money: this.data.money
          },
        })
        db.collection('record').add({
          data: postdata
        }).then(res => {
          Notify({
            type: 'success',
            message: '修改'
          });
        })
      } else {
        this.setData({
          loading: true
        });
        let postdata = {
          type: this.data.type,
          recordtype: this.data.recordtype,
          didi: this.data.didi,
          wuliao: this.data.wuliao,
          yongtu: this.data.yongtu,
          date: this.data.date,
          remarks: this.data.message,
          money: this.data.type == '0' ? this.data.money * -1 : Number(this.data.money),
          userid: wx.getStorageSync('id'),
          icon: this.data.icondata
        }
        console.log(postdata)
        db.collection('record2').add({
          data: postdata
        }).then(res => {

          this.shuju()
        })
        db.collection('record').add({
          data: postdata
        }).then(res => {
          Notify({
            type: 'success',
            message: '增加成功'
          });
        })

      }


    }

  }

})